package com.tengju.motivation.domain.model.performance;

import com.tengju.motivation.domain.shared.DomainException;
import com.tengju.motivation.domain.shared.DomainExceptionCode;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

/**
 * @author: 李大仙
 * @date: 2021/7/22 5:02 下午
 */
public class MotivationActivityConfigFactory {

    public static MotivationActivityConfig create(String title,
                                                  Integer objectType,
                                                  String remark,
                                                  LocalDateTime startTime,
                                                  LocalDateTime endTime,
                                                  String staffName) {
        checkParam(title, objectType, startTime, endTime);
        return new MotivationActivityConfig(null,
                title,
                MotivationActivityConfigObjectTypeEnum.form(objectType),
                remark,
                startTime, endTime,
                staffName, staffName);
    }

    public static void checkParam(String title, Integer objectType, LocalDateTime startTime, LocalDateTime endTime) {
        if (StringUtils.isEmpty(title)) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA, "活动名称不能为空");
        }
        MotivationActivityConfigObjectTypeEnum objectTypeEnum = MotivationActivityConfigObjectTypeEnum.form(objectType);
        if (objectTypeEnum == null) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA, "活动类型不正确");
        }
        if (startTime == null) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA, "开始时间不能为空");
        }
        if (endTime == null) {
            throw DomainException.error(DomainExceptionCode.INVALID_DATA, "结束时间不能为空");
        }
        if (startTime.isEqual(endTime) || startTime.isAfter(endTime)){
            throw DomainException.error(DomainExceptionCode.INVALID_DATA, "开始、结束时间异常");
        }
    }
}
